package com.wjh.serivce.impl;

import com.wjh.common.Constants;
import com.wjh.user.request.UpdateAmountRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSON;
import com.wjh.base.ResponseApiCode;
import com.wjh.base.ResponseApiData;
import com.wjh.feign.IOrderFeignClient;
import com.wjh.feign.IUserFeignClient;
import com.wjh.order.dto.OrderDetailDto;
import com.wjh.order.request.UpdateOrderPayStateRequest;
import com.wjh.serivce.ICommonService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author wenjianhai
 * @date 2023/6/26
 * @since JDK 17
 */
@Slf4j
@Service
public class CommonServiceImpl implements ICommonService {

	@Autowired
	private IOrderFeignClient orderFeignClient;

	@Autowired
	private IUserFeignClient userFeignClient;

	/**
	 * 查询订单及其详情
	 *
	 * @param orderGuid 订单Guid
	 * @return OrderDetailDto
	 * @date 2023-06-28
	 * @author wenjianhai
	 * @since JDK 17
	 */
	@Override
	public OrderDetailDto queryOrderAndDetails(String orderGuid) {
		if (StringUtils.isBlank(orderGuid)) {
			log.info("查询订单及其详情,订单Guid为空");
			return null;
		}
		ResponseApiData<OrderDetailDto> response = orderFeignClient.queryOrderAndDetails(orderGuid);

		log.info("查询订单及其详情,订单Guid:{}, 返回结果:{}", orderGuid, JSON.toJSONString(response));

		if (response == null || !ResponseApiCode.SUCCESS_CODE.equals(response.getCode())) {
			log.error("查询订单及其详情-失败.订单Guid:{}, 返回结果:{}", orderGuid, JSON.toJSONString(response));
			throw new RuntimeException("查询订单及其详情失败");
		}
		return response.getData();
	}

	/**
	 * 更新订单的支付状态
	 *
	 * @param request 请求参数
	 * @param tip     功能名称
	 * @date 2023-06-29
	 * @author wenjianhai
	 * @since JDK 17
	 */
	@Override
	public void updateOrderPayState(UpdateOrderPayStateRequest request, String tip) {
		log.info(tip + ",更新订单的支付状态-开始.请求参数:{}", JSON.toJSONString(request));

		if (StringUtils.isBlank(request.getOrderGuid())) {
			log.info(tip + ",更新订单的支付状态,订单Guid为空");
			return;
		}
		if (request.getPayState() == null) {
			log.info(tip + ",更新订单的支付状态,支付状态为空");
			return;
		}
		ResponseApiData<?> response = orderFeignClient.updatePayState(request);

		log.info(tip + ",更新订单的支付状态-结束.请求参数:{}, 返回结果:{}", JSON.toJSONString(request), JSON.toJSONString(response));

		if (response == null || !ResponseApiCode.SUCCESS_CODE.equals(response.getCode())) {
			log.error(tip + ",更新订单的支付状态-失败.请求参数:{}, 返回结果:{}", JSON.toJSONString(request), JSON.toJSONString(response));
			String errorMsg = response == null ? "更新订单的支付状态失败" : "更新订单的支付状态失败:" + response.getMessage();
			throw new RuntimeException(errorMsg);
		}
	}

	/**
	 * 更新用户可用金额
	 *
	 * @param request 请求参数
	 * @date 2023-06-29
	 * @author wenjianhai
	 * @since JDK 17
	 */
	@Override
	public void updateUserAmount(UpdateAmountRequest request) {
		log.info(request.getTip() + ",更新用户可用金额-开始.请求参数:{}", JSON.toJSONString(request));
		if (StringUtils.isBlank(request.getUserCode())) {
			log.info(request.getTip() + ",更新用户可用金额,用户编码为空");
			return;
		}
		if (request.getAmount() == null || request.getAmount().longValue() == Constants.NUM_ZERO) {
			log.info(request.getTip() + ",更新用户可用金额,金额为空或0");
			return;
		}
		if (request.getFlag() == null) {
			log.info(request.getTip() + ",更新用户可用金额,标识为空");
			return;
		}
		ResponseApiData<?> response = userFeignClient.updateAmount(request);

		log.info(request.getTip() + ",更新用户可用金额-结束.请求参数:{}, 返回结果:{}", JSON.toJSONString(request), JSON.toJSONString(response));

		if (response == null || !ResponseApiCode.SUCCESS_CODE.equals(response.getCode())) {
			log.error(request.getTip() + ",更新用户可用金额-失败.请求参数:{}, 返回结果:{}", JSON.toJSONString(request), JSON.toJSONString(response));
			String errorMsg = response == null ? "更新用户可用金额失败" : "更新用户可用金额失败:" + response.getMessage();
			throw new RuntimeException(errorMsg);
		}
	}
}
